package com.example.demo.Mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.example.demo.entity.Role;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface RoleMapper extends BaseMapper<Role> {

    /**
     * 查询所有热门角色，只返回首页展示需要的字段
     * @return 热门角色列表
     */
    @Select("SELECT id,name, description, avatar FROM character WHERE is_popular = true AND is_public = true ORDER BY create_time DESC")
    @Results({
            @Result(property = "id", column = "id"),
            @Result(property = "name", column = "name"),
            @Result(property = "description", column = "description"),
            @Result(property = "avatar", column = "avatar")
    })
    List<Role> getPopularCharacters();

    /**
     * 根据分类查询角色
     * @param category 角色分类
     * @return 角色列表
     */
    @Select("SELECT * FROM character WHERE category = #{category} AND is_public = true ORDER BY create_time DESC")
    List<Role> getRolesByCategory(@Param("category") String category);

    /**
     * 根据用户ID查询该用户创建的角色
     * @param userId 用户ID
     * @return 角色列表
     */
    @Select("SELECT * FROM character WHERE user_id = #{userId} ORDER BY create_time DESC")
    List<Role> getRolesByUserId(@Param("userId") Long userId);

    /**
     * 获取所有公开角色
     * @return 所有公开角色列表
     */
    @Select("SELECT * FROM character WHERE is_public = true ORDER BY create_time DESC")
    List<Role> getAllPublicRoles();

    /**
     * 根据用户ID和角色ID获取角色的voice配置
     * @param userId 用户ID
     * @param roleId 角色ID
     * @return 角色的voice配置
     */
    @Select("SELECT voice FROM character WHERE id = #{roleId} AND user_id = #{userId}")
    String getRoleVoiceByUserIdAndRoleId(@Param("userId") Long userId, @Param("roleId") Long roleId);

    /**
     * 根据用户ID查询其拥有的所有角色（通过用户角色关联表）
     * @param userId 用户ID
     * @return 用户拥有的角色列表
     */
    @Select("SELECT r.* FROM character r JOIN user_role ur ON r.id = ur.role_id WHERE ur.user_id = #{userId} ORDER BY r.create_time DESC")
    List<Role> getOwnedRolesByUserId(@Param("userId") Long userId);
}